package com.pearadmin.pro.modules.training.controller;

import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.aop.annotation.Repeat;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.sys.param.SysConfigRequest;
import com.pearadmin.pro.modules.training.entity.LaboratoryInfo;
import com.pearadmin.pro.modules.training.entity.TrainingConsumable;
import com.pearadmin.pro.modules.training.entity.TrainingEquipment;
import com.pearadmin.pro.modules.training.entity.vo.PageTrainingConsumableVO;
import com.pearadmin.pro.modules.training.service.LaboratoryInfoService;
import com.pearadmin.pro.modules.training.service.TrainingConsumableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Api(tags = {"实训耗材"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "tcc")
public class TrainingConsumableController extends BaseController {

    @Resource
    private TrainingConsumableService trainingConsumableService;
    @Resource// 实训室
    private LaboratoryInfoService laboratoryInfoService;

    @Resource
    private UserContext userContext;

    /**
     * 实训室耗材列表
     *
     * @param request 查询参数
     * @return {@link Result}
     */
    @GetMapping("page")
    @Log(title = "实训室耗材列表")
    @ApiOperation(value = "实训室耗材列表")
    public Result page(PageTrainingConsumableVO request){
        PageResponse<TrainingConsumable> page = trainingConsumableService.page(request);
        List<TrainingConsumable> record = page.getRecord();
        record.forEach(trainingEquipment -> {
            Integer liId = trainingEquipment.getLiId();
            LaboratoryInfo byId = laboratoryInfoService.getById(liId);
            trainingEquipment.setLainName(byId.getLainName());
        });
        page.setRecord(record);
        return success(page);
    }
    /**
     * 新增实训室耗材
     *
     * @param trainingConsumable
     */
    @Repeat
    @PostMapping("save")
    @Log(title = "新增实训室耗材")
    @ApiOperation(value = "新增实训室耗材")
    public Result save(@RequestBody TrainingConsumable trainingConsumable) {
        trainingConsumable.setCreateName(userContext.getUserId());
        trainingConsumable.setCreateTime(LocalDateTime.now());

        return auto(trainingConsumableService.insertInto(trainingConsumable));
    }
    @Repeat
    @PostMapping("Update")
    @Log(title = "修改实训室耗材")
    @ApiOperation(value = "修改实训室耗材")
    public Result Update(@RequestBody TrainingConsumable trainingConsumable) {
        trainingConsumable.setUpdateName(userContext.getUserId());
        trainingConsumable.setUpdateTime(LocalDateTime.now());
        return auto(trainingConsumableService.updateById(trainingConsumable));
    }
    /**
     * 删除实训室耗材
     *
     * @param id 实训室耗材编号
     */
    @DeleteMapping("remove")
    @Log(title = "删除实训室耗材")
    @ApiOperation(value = "删除实训室耗材")
    public Result remove(@RequestParam String id){
        return auto(trainingConsumableService.removeById(id));
    }

    /**
     * 删除实训室耗材
     *
     * @param ids 实训室耗材编号
     * */
    @DeleteMapping("removeBatch")
    @Log(title = "删除实训室耗材")
    @ApiOperation(value = "删除实训室耗材")
    public Result removeBatch(@RequestParam List<String> ids) { return auto(trainingConsumableService.removeByIds(ids)); }


}
